"""
教师视图层
"""
from interface import common_interface, teacher_interface
from lib.utils import prettyprint, loop_input, login_auth

login_user = {'username': ''}


def login():
    """教师登录"""
    username = input('请输入教师姓名:').strip()
    password = input('请输入密码:').strip()

    success, msg = common_interface.login_interface(username, password, 1)  # 调用公共登录接口
    print(msg)  # 打印消息
    if success:  # 登录成功
        login_user['username'] = username


@login_auth(login, login_user)
def select_course():
    """选择课程"""
    course_name_list = common_interface.list_course_interface()
    if not course_name_list:  # 学校课程为空
        print('当前没有课程，请联系管理员！')
        return

    # 选择课程
    index = int(loop_input('请输入选择的课程序号:', '输入错误,请重新输入！',
                           lambda x, y: int(x) in range(1, len(y) + 1) if x.isdigit() else False, course_name_list))
    course_name = course_name_list[index - 1]

    # 调用选择课程接口
    success, msg = teacher_interface.select_course_interface(login_user['username'], course_name)
    print(msg)  # 打印消息


@login_auth(login, login_user)
def check_course():
    """查看课程"""
    # 调用查看课程接口
    success, msg = teacher_interface.check_course_interface(login_user['username'])
    print(msg)  # 打印消息


@login_auth(login, login_user)
def change_score():
    """修改分数"""
    success, msg = teacher_interface.check_course_interface(login_user['username'])
    if not success:  # 学校课程为空
        print('当前没有课程，请联系管理员！')
        return

    course_name_list = teacher_interface.list_course_from_teacher(login_user['username'])
    # 选择课程
    index = int(loop_input('请输入选择的课程序号:', '输入错误,请重新输入！',
                           lambda x, y: int(x) in range(1, len(y) + 1) if x.isdigit() else False, course_name_list))
    course_name = course_name_list[index - 1]

    # 打印学生
    student_name_list = common_interface.list_student_score_interface(course_name)

    if not student_name_list:  # 课程下没有学生
        print('当前课程下没有学生！')
        return

    index = int(loop_input('请输入要修改分数的学生序号:', '输入错误,请重新输入！',
                           lambda x, y: int(x) in range(1, len(y) + 1) if x.isdigit() else False, student_name_list))
    student_name = student_name_list[index - 1]

    # 修改分数
    score = loop_input('请输入修改后的分数:', '分数必须为0~100的数字！请重新输入！',
                       lambda x: int(x) <= 100 if x.isdigit() else False)
    # 调用查看课程接口
    success, msg = teacher_interface.change_score_interface(course_name, student_name, score)
    print(msg)


teacher_dict = {
    '1': login,
    '2': select_course,
    '3': check_course,
    '4': change_score,
    'T': None,
    'Q': None
}


def run():
    while True:
        username = login_user["username"] if login_user["username"] else '未登录'  # 当前登录的用户名
        prettyprint(
            f'当前教师:{username}',
            ['编号', '功能'],
            [['【1】', '教师登录'],
             ['【2】', '选择课程'],
             ['【3】', '查看课程'],
             ['【4】', '修改分数']],
            8)
        order = loop_input('请输入功能编号[Q返回][T登出]:', '输入错误请重新输入！', lambda x, y: x in y, teacher_dict)

        if order == 'Q':  # 返回主视图
            login_user['username'] = ''  # 将当前用户设置为空
            return

        if order == 'T':  # 退出登录
            if username != '未登录':
                print('无法退出登录，请先登录！')
            else:
                login_user['username'] = ''  # 将当前用户设置为空
            continue

        teacher_dict[order]()
        input('按回车继续...')
